<template>
   <div id="home">
      <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
     <tab-control class="tab-control2" v-show="isTabFixed" :titles="['流行','新款','精选']" @tabClick="tabClick" ref="tabcontrol" ></tab-control>
     <scroll class="content" ref="scroll" :prototype1="3"
              :pull-up-load="true"  @pullingUp="loadmore"  @scroll="contentScroll">
        <home-swiper :banners="banners" @swiperimgeLoad="swiperimgeLoad"></home-swiper>
        <recommed-view :recommends="recommends"></recommed-view>
        <feature></feature>
        <tab-control class="tab-control" :titles="['流行','新款','精选']" @tabClick="tabClick" ref="tabcontrol2" ></tab-control>
        <good-list :goods="show"></good-list>
      </scroll>
     <!--组件不能直接监听点击事 件-->
     <backtop   v-show="isShowBackTop" @click.native="backtop"></backtop>
   </div>
</template>

<script>

  import HomeSwiper from './chiildComps/HomeSwiper';
  import RecommedView from "./chiildComps/RecommedView";
  import Feature from "./chiildComps/Feature";
  /*事件监听的相关方法
   *  传入函数 和时间
   * */
  import  {debounce} from  "../../components/common/utils/utils.js"

  import NavBar from "components/common/navbar/NavBar"
  import tabControl from "components/content/tapControl/tabControl";
  import  GoodList from "components/content/goods/goodslist"
  import  Scroll from "components/common/scroll/Scroll"
  import Backtop from "../../components/content/backtop/backtop";

  import {getHomeMultidata,getHomeCode} from "../../network/home";

  export default {
    name: 'Home',
    components: {
      Backtop,
      RecommedView,
      HomeSwiper,
      Feature,

      NavBar,
      tabControl,
      GoodList,
      Scroll
    },
    data() {
      return {
        banners: [],
        recommends: [],
        goods: {
          'pop': {page: 0, list: []},
          'new': {page: 0, list: []},
          'sell': {page: 0, list: []}
        },
        currentType:'pop',
        isShowBackTop:true,
        tabOffsetTop:0,
        isTabFixed:false,
        savey:0
      }
    },
    //保持原来的位置
    activated() {
       this.$refs.scroll.scrollTo(0,this.savey)
       this.$refs.scroll.refresh()
    },
    deactivated() {
      //保存y值
      this.savey=this.$refs.scroll.getScrollY()
    },
    created() {
      //create 里面只写主要的逻辑代码
      //请求商品数据
      this.getHomeMultidata();
      this.getHomeCode('pop');
      this.getHomeCode('new');
      this.getHomeCode('sell');
    },
    computed:{
      show(){
       return this.goods[this.currentType].list
      }
    },
    methods: {
      /*用于事件监听*/
      tabClick(index){

        switch (index) {
            case 0:
              this.currentType='pop';
                break;
             case  1:
               this.currentType='new';
               break;
             case 2:
               this.currentType="sell"
         }
         //让两个tabcontrol保持一致
         this.$refs.tabcontrol.currentIndex=index
         this.$refs.tabcontrol2.currentIndex=index
      },
      backtop(){
        this.$refs.scroll.scrollTo(0,0,600)
      },
      //上啦加载更多
      loadmore(){
         this.getHomeCode(this.currentType);
      },
      /*用于网络请求*/
      getHomeMultidata() {
        //第一步请求多个数据
        getHomeMultidata().then(
          res => {
            this.banners = res.data.banner.list;
            this.recommends = res.data.recommend.list;
          })
      },
      //方法复用
      getHomeCode(type) {
        //动态获取网络请求时需要
        const page=this.goods[type].page+1
        getHomeCode(type,page).then(res => {
            this.goods[type].list.push(...res.data.list)
           this.goods[type].page+=1
          //完成下拉加载更多
          this.$refs.scroll.scroll.finishPullUp();
        })
      },
      contentScroll(position){
        //判断BackTop是否显示
          this.isShowBackTop=-position.y>1000
        //判断是否吸顶
         this.isTabFixed=(-position.y)>this.tabOffsetTop
      },
      swiperimgeLoad(){
        //获取contraol的offsetTop
        //所有的组件都有一个$el;用于获取组件中的元素
        this.tabOffsetTop = this.$refs.tabcontrol2.$el.offsetTop;
        console.log(this.tabOffsetTop);
      },
    },
    //挂载完组件的生命周期函数
    mounted() {
      /*事件监听的相关方法*/
        const  refresh=debounce(this.$refs.scroll.refresh,100)
      //监听图片加载
      this.$bus.$on('itemImgLoad', ()=>{
        //防止这个函数执行很多次
            refresh()
        })
    }
  }
</script>
<style scoped>
  #home{
    /*vh是视口高度*/
    height: 100vh;
    position: relative;
  }
.home-nav{
   background-color: var(--color-tint);
  color: white;
  font: bold 16px/44px '微软雅黑';
  /*这个属性是在使用浏览器原生滚动时使用*/
/*  position: fixed;
  left: 0;
  right: 0;
  top: 0;*/
  z-index: 9;
}
  .tab-control{
    position: sticky;
    z-index: 9;
    top: 4px;
  }
  .content{
    overflow: hidden;
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }
  .tab-control2{
    position: relative;
    z-index:1;
  }
/*  .content{
    height: calc(100% - 93px);
    overflow: hidden;
    margin-top: 44px;
  }*/
</style>
